package com.genexususa.eo.bing.data;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import org.xml.sax.SAXException;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ErrorWSCollection")
public class ErrorWSCollection {
    @XmlElement(name = "Error")
    public ArrayList<ErrorWS> errorCollection;

    public ErrorWSCollection() {
        this.errorCollection = new ArrayList<ErrorWS>();
    }

    public void addServiceExceptionError(ServiceException e) {
        this.errorCollection.add(new ErrorWS(-1, e.getMessage()));
    }

    public void addIllegalArgumentExceptionError(IllegalArgumentException e) {
        this.errorCollection.add(new ErrorWS(-5, e.getMessage()));
    }

    public void addGxExceptionError(GxItemException e) {
        this.errorCollection.add(new ErrorWS(-6, e.getMessage()));
    }

    public void addParserConfigurationExceptionError(ParserConfigurationException e) {
        this.errorCollection.add(new ErrorWS(-2, e.getMessage()));
    }

    public void addSAXExceptionError(SAXException e) {
        this.errorCollection.add(new ErrorWS(-3, e.getMessage()));
    }

    public void addApplicationFaultError(ApplicationFault fault) {
        if (fault.getFaultCode() != null) {
            this.errorCollection.add(new ErrorWS(0, fault.getFaultString()));
        }
    }

    public void addOperationErrors(OperationError[] errors) {
        for (int i=0; i<errors.length; i++) {
            this.errorCollection.add(new ErrorWS(errors[i].getCode(), errors[i].getMessage()));
        }
    }

    public void addBatchError(BatchError error) {
        this.errorCollection.add(new ErrorWS(error.getCode(), error.getMessage()));
    }

    public void addEditorialError(EditorialError error) {
        this.errorCollection.add(new ErrorWS(error.getCode(), error.getMessage()));
    }

    public void addAdApiFaultDetailError(AdApiFaultDetail fault) {
        AdApiError[] errors = fault.getErrors();
        for (int i=0; i<errors.length; i++) {
            this.errorCollection.add(new ErrorWS(errors[i].getCode(), errors[i].getMessage()));
        }
    }

    public void addRemoteExceptionError(RemoteException e) {
        this.errorCollection.add(new ErrorWS(-7, e.getMessage()));
    }

    public void addIOExceptionError(IOException e) {
        this.errorCollection.add(new ErrorWS(-4, e.getMessage()));
    }

    public void addGxItemExceptionError(GxItemException e) {
        this.errorCollection.add(new ErrorWS(-6, e.getMessage()));
    }
}
